<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-04-02
 * Time: 01:13
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\model\MessageUser;
use app\api\service\Token as TokenService;
use app\api\validate\Paging;
use app\api\model\Message as MessageModel;

class Message extends BaseController
{
    protected $beforeActionList = [
        'checkPrimaryScope' => ['only' => 'getMessageList,getMessageInfo']
    ];
    /**
     * 获取信息列表
     */
    public function getMessageList($page,$limit)
    {
        (new Paging())->goCheck();
        $uid = TokenService::getCurrentUid();
        $query = MessageUser::with('message')
            ->where('user_id',$uid)
            ->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取详情
     */
    public function getMessageInfo($id)
    {
        TokenService::getCurrentUid();
        $info = MessageModel::where('id',$id)->find();
        MessageUser::where('message_id',$info['id'])->update([
            'read_status' => 2
        ]);
        return $info;
    }
}